package net.projecteuler.programs.program0010;

import net.projecteuler.programs.TimeLogger;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Summation of primes
 *
 * The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
 *
 * Find the sum of all the primes below two million.
 *
 * Created by zhaojjd on 2016/3/21.
 */
public class Program0010 {

    List<Integer> primes = new LinkedList<Integer>();
//    List<Integer> primes = new ArrayList<Integer>(400000);

    public long sum(int count){
        long rst = 2;

//        primes.add(2);

        for (int i = 3; i <= count; i+=2) {
            if(hasPrime(i)){
                continue;
            }else {
                primes.add(i);
                rst += i;
            }
        }
        System.out.println("primes.size() = " + primes.size());
        return rst;
    }

    private boolean hasPrime(int i) {
        for (Integer prime : primes) {
            if(i % prime == 0){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Program0010 p = new Program0010();

        TimeLogger.tag();
        System.out.println("p.sum(2000000) = " + p.sum(2000000));
        TimeLogger.print();
    }

}
